﻿using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IceDog.JsonDotNet.Model.NewSerializationBinder
{
    public class KnownTypesBinder : ISerializationBinder
    {
        public IList<Type> KnownTypes { get; set; }

        /// <summary>
        /// 反序列化的时候根据$type值找到具体的type
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public Type BindToType(string assemblyName, string typeName)
        {
            return KnownTypes.SingleOrDefault(t => t.Name == typeName);
        }

        public void BindToName(Type serializedType, out string assemblyName, out string typeName)
        {
            assemblyName = null;
            //一般根据类型名就能找到对应的模型即可，不需要限制程序集
            //assemblyName = serializedType.Assembly.GetName().Name;
            typeName = serializedType.Name;
        }
    }
}
